package com.leetcode;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: study
 * @className: Solution349
 * @description: TODO
 * @author: kukuxiahuni
 * @create: 2020-02-06 16:11
 **/
public class Solution690 {
    private final static class Employee {
        private int id;
        private int importance;
        public List<Employee> subordinates;
    }

    private Map<Integer, Employee> map;

    public int getImportance(List<Employee> employees, int id) {

        if (employees == null || employees.isEmpty()) {
            return 0;
        }

        this.map = employees.stream().collect(Collectors.toMap(e -> e.id, t -> t));

        return dfs(id);
    }

    public int dfs(int id) {
        Employee e = this.map.get(id);
        int ans = e.importance;

        for (Employee subE : e.subordinates) {
            ans += dfs(subE.id);
        }
        return ans;
    }

}
